
;; The following customization will tell remember to use org files as
;; target, and to create annotations compatible with Org-mode links.




;; These are just some general org-mode customizations to make org better. I don’t like org inserting blank lines, and I do like ido. org-insert-heading-respect-content will make new headings appear after the content for the current one, which is usually the behavior I want. Allowing return to follow links makes navigation much easier. org-special-ctrl-a/e will rebind ctrl-a and ctrl-e to make more sense in org mode, going to the beginning and end of the heading instead of the current line. This is actually a bit inaccurate - ctrl-a cycles between beginning of heading and beginning of line, which helps with speed commands. Enabling speed commands will rebind certain one-letter keybindings when the cursor is at the beginning of the row - most notably ‘t’ for org-todo instead of ‘C-c C-t’, a huge increase in efficiency.
;; taken from http://nflath.com/2010/03/org-mode-2/

;; (setq org-completion-use-ido t)
;; (setq org-blank-before-new-entry '((heading . nil) (plain-list-item . nil)))
;; (setq org-completion-use-ido t)
;; (setq org-insert-heading-respect-content t)
;; (setq org-return-follows-link t)
;; (setq org-special-ctrl-a/e t)
;; (setq org-use-speed-commands t)
;; (setq org-speed-commands-user nil)


;; Org mode allows you to clock the time you spend on specific tasks in a project. When you start working on an item, you can start the clock. When you stop working on that task, or when you mark the task done, the clock is stopped and the corresponding time interval is recorded. It also computes the total time spent on each subtree of a project. And it remembers a history or tasks recently clocked, to that you can jump quickly between a number of tasks absorbing your time.
;; 
;; To save the clock history across Emacs sessions, use 
;; 

;(setq org-clock-persist 'history)
(org-clock-persistence-insinuate)



;; The following lines are always needed.  Choose your own keys.
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
;;

;;
;;
;; Experiments with automatic date creation on new nodes
;; C-c i inserts the time stamp
;; C-u M-x apropos org-insert-time-stamp ... finds help
;;

(defun bh/insert-inactive-timestamp ()
  (interactive)
  (save-excursion
    ;(insert "\n")
    (org-cycle)
    (org-insert-time-stamp nil t t nil nil nil)))

(global-set-key (kbd "C-c i") 'bh/insert-inactive-timestamp)



;;(add-hook 'org-insert-heading-hook 'bh/insert-inactive-timestamp)

;;; Stuff from bernt hansen begins
;; Change task state to STARTED from TODO when clocking in

(defun bh/clock-in-to-started (kw)
  "Switch task from TODO to STARTED when clocking in"
  (if (and (string-equal kw "TODO")
           (not (string-equal (buffer-name) "*Remember*")))
      "STARTED"
    nil))

;(setq org-clock-in-switch-to-state (quote bh/clock-in-to-started))

;; Start clock in a remember buffer and switch back to previous clocking task on save
 (add-hook 'remember-mode-hook 'org-clock-in 'append)
 (add-hook 'org-remember-before-finalize-hook 'bh/clock-in-interrupted-task)

;; Keep clocks running
; (setq org-remember-clock-out-on-exit nil)

(defun bh/clock-in-task-by-id (id)
  "Clock in a task by id"
  (require 'org-id)
  (save-restriction
    (widen)
    (org-with-point-at (org-id-find id 'marker)
      (org-clock-in nil))))

(defun bh/clock-in-organization-task ()
  (interactive)
  (bh/clock-in-task-by-id "437c2cde-fbf0-491f-92ba-51bae487b338"))

(defun bh/clock-in-read-mail-and-news-task ()
  (interactive)
  (bh/clock-in-task-by-id "85c2e69b-6f37-4236-8896-4f7dd86047c1"))

(defun bh/clock-in-interrupted-task ()
  "Clock in the interrupted task if there is one"
  (interactive)
  (if (and (not org-clock-resolving-clocks-due-to-idleness)
           (marker-buffer org-clock-marker)
           (marker-buffer org-clock-interrupted-task))
      (org-with-point-at org-clock-interrupted-task
        (org-clock-in nil))
    (org-clock-out)))

;; Remove empty CLOCK drawers on clock out
(defun bh/remove-empty-drawer-on-clock-out ()
  (interactive)
  (save-excursion
    (beginning-of-line 0)
    (org-remove-empty-drawer-at "CLOCK" (point))))

(add-hook 'org-clock-out-hook 'bh/remove-empty-drawer-on-clock-out 'append)




;;; Stuff from bernt hansen ends


(add-hook 'org-mode-hook 'turn-on-font-lock)  ; Org buffers only
;;;With this setup, all files with extension ‘.org’ will be put into Org mode. 

;; set up org publishing following: 
;; http://orgmode.org/worg/org-tutorials/org-publish-html-tutorial.php
;;







(define-key mode-specific-map [?a] 'org-agenda)

(eval-after-load "org"
  '(progn
     (define-prefix-command 'org-todo-state-map)

     (define-key org-mode-map "\C-cx" 'org-todo-state-map)

     (define-key org-todo-state-map "x"
       #'(lambda nil (interactive) (org-todo "CANCELLED")))
     (define-key org-todo-state-map "d"
       #'(lambda nil (interactive) (org-todo "DONE")))
     (define-key org-todo-state-map "f"
       #'(lambda nil (interactive) (org-todo "DEFERRED")))
     (define-key org-todo-state-map "l"
       #'(lambda nil (interactive) (org-todo "DELEGATED")))
     (define-key org-todo-state-map "s"
       #'(lambda nil (interactive) (org-todo "STARTED")))
     (define-key org-todo-state-map "w"
       #'(lambda nil (interactive) (org-todo "WAITING")))

     (define-key org-agenda-mode-map "\C-n" 'next-line)
     (define-key org-agenda-keymap "\C-n" 'next-line)
     (define-key org-agenda-mode-map "\C-p" 'previous-line)
     (define-key org-agenda-keymap "\C-p" 'previous-line)))

;;(require 'remember) not required in emacs 23
(org-remember-insinuate) ;; enable remember

(add-hook 'remember-mode-hook 'org-remember-apply-template)

(define-key global-map [(control meta ?r)] 'remember)


;; I ran into problems trying to make the wiegley's org-stuff work with my todo list.  So I just started taking code from his emacs.el file on 4-27-10.  I seem to get this stuff to work.  And I put this after the load of wiegley-org.el, which also seems to contain some duplicate defintions that may be overridden by what i inserted below.  I have to figure out what he has done, and in the process i will learn quite a bit about org-mode todo lists.



(defvar org-my-archive-expiry-days 2
  "The number of days after which a completed task should be auto-archived.
This can be 0 for immediate, or a floating point value.")

(defun org-my-archive-done-tasks ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (let ((done-regexp
           (concat "\\* \\(" (regexp-opt org-done-keywords) "\\) "))
          (state-regexp
           (concat "- State \"\\(" (regexp-opt org-done-keywords)
                   "\\)\"\\s-*\\[\\([^]\n]+\\)\\]")))
      (while (re-search-forward done-regexp nil t)
        (let ((end (save-excursion
                     (outline-next-heading)
                     (point)))
              begin)
          (goto-char (line-beginning-position))
          (setq begin (point))
          (if (re-search-forward state-regexp end t)
              (let* ((time-string (match-string 2))
                     (when-closed (org-parse-time-string time-string)))
                (if (>= (time-to-number-of-days
                         (time-subtract (current-time)
                                        (apply #'encode-time when-closed)))
                        org-my-archive-expiry-days)
                    (org-archive-subtree)))
            (goto-char end)))))
    (save-buffer)))

(setq safe-local-variable-values (quote ((after-save-hook archive-done-tasks))))

(defalias 'archive-done-tasks 'org-my-archive-done-tasks)


